import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model, ObjectId, Types } from 'mongoose';
import { User, UserDocument } from 'src/schemas/user.schema';
import { CreateUserItem } from './interface/user.interface';

@Injectable()
export class UserService {
  constructor(@InjectModel(User.name) private userModel: Model<UserDocument>) {}
  async findOne(username: string) {
    return await this.userModel.findOne({
      username,
    });
  }
  async findOneById(_id: Types.ObjectId) {
    return await this.userModel.findOne({
      _id,
    });
  }
  async create(item: CreateUserItem) {
    const { username, password, nickname } = item;
    const model = new this.userModel({
      username,
      password,
      info: {
        nickname,
      },
      address_book: [],
    });
    await model.save();
  }
}
